<?php
$uid=$pwd="";
if($_SERVER["REQUEST_METHOD"]=="POST"){
    $uid=test_input($_POST["uid"]);
    $pwd=test_input($_POST["pwd"]);
    $host="localhost";
    $user="root";
    $password="wcn4911.";
    $database="muban_php";
    $conn=new mysqli($host,$user,$password,$database);
    if($conn->connect_error){
        die($conn->connect_error);
    }
    else{
        $sql="select pwd from user where uid='".$uid."'";
        $result=$conn->query($sql);
        if($result->num_rows==1){
            $row = $result->fetch_assoc();
            if($pwd==$row["pwd"]){
                setcookie("user",$uid);
                echo "<script>location.href='index.php'</script>";
                //设置cookie
                //跳转主页
            }
            
        }
        else{
            die("error");
        }
    }
}

function test_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
?>
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <link type="text/css" href="css/base.css">
        <script src="js/base.js"></script>
        <title>登录</title>
    </head>
    <body>
        <div class="main">
            
            <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
                <input type="text" name="uid" placeholder="用户名" required="required"/>
                <input type="password" name="pwd" placeholder="密码" required="required"/>
                <input type="submit" value="登录">
            </form>
            
        </div>
    </body>
</html>